package com.stedu.mall.goodsservice.controller;

import com.stedu.mall.common.bean.Category;
import com.stedu.mall.common.bean.RespBean;
import com.stedu.mall.common.exception.SteduException;
import com.stedu.mall.common.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin
@RestController
@RequestMapping("/category")
public class CategoyController {
    @Autowired
    private CategoryService categoryService;

    //添加
    @PostMapping
    public RespBean insert(@RequestBody Category category) throws SteduException {
        categoryService.insert(category);

        return RespBean.ok("添加成功");
    }
    //根据id删除
    @DeleteMapping("/{id}")
    public RespBean delete(@PathVariable("id") Integer id) throws SteduException {
        categoryService.delete(id);

        return RespBean.ok("删除成功");
    }
    @PutMapping
    public RespBean update(@RequestBody Category category) throws SteduException {
        categoryService.update(category);

        return RespBean.ok("修改成功");
    }
    @GetMapping("/search")
    public  RespBean selectByPage(Category category, Integer pageNum,Integer pageSize){
        Object data =categoryService.selectByCondtion(category, pageNum,pageSize );

        return RespBean.ok("", data);
    }

    @GetMapping("/{id}")
    public RespBean selectById(@PathVariable("id") Integer id)  {

        return RespBean.ok("",categoryService.selectById(id));
    }

    @GetMapping("/allparent")
    public  RespBean selectAllParent(){
        Category condition =new Category();
        condition.setParentId(0);
        Object data = categoryService.selectByCondtion(condition, null, null);
      return   RespBean.ok("",data);
    }


}
